package JAVA300.第十二章网络编程;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * 在线聊天室：服务器端
 * ---实现多个客户可以正常收发数次消息
 * 多线程基础上添加封装
 * 1。分析：线程里主要内容就是声明流-接收数据-返回数据-释放资源，操作封装起来
 *2。客户端进行读写分离
 * ----只能自言自语
 */

public class web05_TCP6_Chat5Server {
    public static void main(String[] args) throws IOException {

        System.out.println("--------Server-----------");
        //1。指定端口 使用ServerSocket创建服务器
        //***添加while

        ServerSocket server = new ServerSocket(9796);
            while (true) {
                //2.阻塞式的等待链接 accept
                Socket socket = server.accept();
                System.out.println("一个客户端建立了连接");


                new Thread(new Channel(socket)).start();

//线程封装                new Thread(()->{
//                //3接收消息
//
////         封装进构造器           DataInputStream dis = null;
////                    DataOutputStream dos =null;
////                    try {
////                        dis = new DataInputStream(socket.getInputStream());
////                        dos = new DataOutputStream(socket.getOutputStream());
////                    } catch (IOException e) {
////                        e.printStackTrace();
////                    }
//                    //4返回消息
//
//                    //线程run操作
//                boolean isRunning = true;
//                while (isRunning) {
//                    String msg = null;
//                    try {
//                        //接收消息
//       //                 msg = dis.readUTF();
//                        //发送消息
//       //                 dos.writeUTF(msg);
//       //                 dos.flush();
//                    } catch (IOException e) {
//                        e.printStackTrace();
//                        //出问题
//                        isRunning = false;
//                    }
//
//
//                }
//                //5释放资源-借助带有可变参数的工具类函数进行封装
//
//        }).start();

    }

    }

    static class Channel implements Runnable{
        private DataInputStream dis;
        private DataOutputStream dos;
        private Socket socket;
        private boolean isRunning;

        public Channel(Socket socket) {
            this.socket = socket;
            isRunning =true;

            try {
                dis = new DataInputStream(socket.getInputStream());
                dos = new DataOutputStream(socket.getOutputStream());
            } catch (IOException e) {
               // e.printStackTrace();
                System.out.println("---error Channel-----");
                //出错了
                release();
            }

        }

        //接收数据
        private String receive(){

            String getMsg ="";//避免空指针
            try {
                getMsg=  dis.readUTF();
            } catch (IOException e) {
                System.out.println("---error receive-----");
                release();
            }
            return getMsg;

        }

        //返回数据
        private void send(String sendMsg){
            try {
                dos.writeUTF(sendMsg);
                dos.flush();
            } catch (IOException e) {
                System.out.println("---error send-----");
                release();
            }



        }

        //释放资源-释放资源-借助带有可变参数的工具类函数进行封装
        private void release(){
            this.isRunning = false;
            web_TCPChatUntils.close(dos,dis,socket);

        }

        @Override
        public void run() {

            while (this.isRunning){
                String getMsg =receive();
                if (!getMsg.equals("")){
                    send(getMsg);
                }
            }

        }
    }
}
